﻿package idv.etrex.display
{
	import flash.display.Graphics;

	/**
	 * 用xml來儲存graphics指令，搭配GraphicsBat使用。
	 * 
	 * @includeExample Demo.txt
	 */
	public class XGraphics implements IGraphics
	{

		/**
		 * 存放XGraphics的容器
		 * XGraphics container
		 */
		public var xml:XML;
		/**
		 * 可傳入xml建立XGraphics物件
		 * @param	xml
		 */
		public function XGraphics(xml:XML = null  ) {
			if (xml == null)
				this.xml = <XGraphics/>;
			else
				this.xml = xml;
		}

		/**
		 * 清除 xml
		 * clear XML
		 */
		public function clearXML():void {
			this.xml = new XML("<XGraphics/>");
		}
		
		/**
		 * 輸出所儲存的xml字串
		 * output xml string
		 */
		public function toString():String {
			return xml.toXMLString();
		}
		
		
		
		// graphics function 
		/**
		 * graphics指令
		 */
		public function beginFill(color:uint, alpha:Number = 1.0):void{
			var xml:XML = new XML('<beginFill color="' + color + '" alpha="' + alpha + '"/>');
			this.xml.appendChild(xml);
		}
		
		/**
		 * graphics指令
		 */
		public function clear():void {
			var xml:XML = new XML('<clear />');
			this.xml.appendChild(xml);
		}
		
		/**
		 * graphics指令
		 */
		public function drawCircle(x:Number, y:Number, radius:Number):void{
			var xml:XML = new XML('<drawCircle x="' + x + '" y="' + y + '" radius="' + radius + '"/>');
			this.xml.appendChild(xml);
		}
		
		/**
		 * graphics指令
		 */
		public function drawEllipse(x:Number, y:Number, width:Number, height:Number):void{
			var xml:XML = new XML('<drawEllipse x="' + x + '" y="' + y + '" width="' + width + '" height="' + height + '"/>');
			this.xml.appendChild(xml);
		}
		
		/**
		 * graphics指令
		 */
		public function drawRect(x:Number, y:Number, width:Number, height:Number):void{
			var xml:XML = new XML('<drawRect x="' + x + '" y="' + y + '" width="' + width + '" height="' + height + '"/>');
			this.xml.appendChild(xml);
		}
		
		/**
		 * graphics指令
		 */
		public function drawRoundRect(x:Number, y:Number, width:Number, height:Number, ellipseWidth:Number, ellipseHeight:Number = NaN):void{
			var xml:XML = new XML('<drawRoundRect x="' + x + '" y="' + y + '" width="' + width + '" height="' + height + '" ellipseWidth="' + ellipseWidth + '" ellipseHeight="' + ellipseHeight + '"/>');
			this.xml.appendChild(xml);
		}
		
		/**
		 * graphics指令
		 */
		public function endFill():void {
			var xml:XML = new XML('<endFill />');
			this.xml.appendChild(xml);
		}
		
		/**
		 * graphics指令
		 */
		public function lineStyle(thickness:Number = NaN, color:uint = 0, alpha:Number = 1.0, pixelHinting:Boolean = false, scaleMode:String = "normal", caps:String = null, joints:String = null, miterLimit:Number = 3):void{
			var xml:XML = new XML('<lineStyle thickness="' + thickness + '" color="' + color + '" alpha="' + alpha + '" pixelHinting="' + pixelHinting + '" scaleMode="' + scaleMode + '" caps="' + caps + '" joints="' + joints + '" miterLimit="' + miterLimit + '"/>');
			this.xml.appendChild(xml);
		}
		
		/**
		 * graphics指令
		 */
		public function moveTo(x:Number, y:Number):void {
			var xml:XML = new XML('<moveTo x="' + x + '" y="' + y + '"/>');
			this.xml.appendChild(xml);
		}
		
		/**
		 * graphics指令
		 */
		public function lineTo(x:Number, y:Number):void{
			var xml:XML = new XML('<lineTo x="' + x + '" y="' + y + '"/>');
			this.xml.appendChild(xml);
		}
	}
	
}